// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import '@/assets/cover.css'
import { Button, Row, Col, Input, InputNumber, Form, FormItem, Container, Header, Dropdown, DropdownItem, DropdownMenu, Badge, Aside, Menu,
  Submenu, MenuItem, Main, Breadcrumb, BreadcrumbItem, Table, TableColumn, Select, Option, DatePicker, Dialog, Upload, Loading,
  Pagination, RadioGroup, Radio, MessageBox, Message, Tag, Rate, Cascader, Checkbox, Tree, Tabs, TabPane } from 'element-ui'
import myBreadcrumb from '@/components/common/myBreadcrumb'
import search from '@/components/common/search'
import axios from 'axios'
import qs from 'qs'

const Components = { Row, Col, Checkbox, Tree, Cascader, Button, Input, Form, InputNumber, FormItem, Container, Header, Dropdown, DropdownItem, DropdownMenu, Badge, Aside, Menu, Submenu, MenuItem, Main, Breadcrumb, BreadcrumbItem, Table, TableColumn, Select, Option, DatePicker, Dialog, Upload, Pagination, RadioGroup, Radio, Tag, Rate, Tabs, TabPane }

Object.keys(Components).forEach(key => {
  Vue.component(Components[key].name, Components[key])
})
Vue.component('myBreadcrumb', myBreadcrumb)
Vue.component('search', search)
Vue.prototype.$Loading = Loading
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$prompt = MessageBox.prompt
Vue.prototype.$message = Message
// axios.defaults.baseURL = 'http://xqz.wecozhiku.com/index.php/Admin'


// axios.defaults.baseURL = 'http://api.xiaoqunzhi.com/Ces'
axios.defaults.baseURL = 'http://api.xiaoqunzhi.com/Admin'

const whiteList = ['/login/loginHandle']
// Add a request interceptor
axios.interceptors.request.use(function (config) {
  // Do something before request is sent
  let session = window.sessionStorage.getItem('sessionId')
  if (!session && whiteList.indexOf(config.url) === -1) {
    Message.error('请先登录')
    router.push('/')
  }
  if (whiteList.indexOf(config.url) === -1) {
    config.headers.key = session
  }
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})

// Add a response interceptor
axios.interceptors.response.use(function (response) {
  // Do something with response data
  if (response.data.status === -1001) {
    Message.error('请先登录')
    router.push('/')
  } else if (response.data.status && Number(response.data.status) !== 1) {
    Message.warning(response.data.msg)
  }
  return response
}, function (error) {
  // Do something with response error
  Message.error('未知错误，请稍后重试！')
  return Promise.reject(error)
})

const whitePath = ['/']
router.beforeEach((to, from, next) => {
  let paths = JSON.parse(window.sessionStorage.getItem('path'))
  if ((paths && paths.indexOf(to.path) !== -1) || whitePath.indexOf(to.path) !== -1) {
    next()
  } else {
    next(false)
  }
})

// Vue.directive('has', {
//   // 当被绑定的元素插入到 DOM 中时……
//   inserted: function (el, binding) {
//     let paths = JSON.parse(window.sessionStorage.getItem('path'))
//     console.log(paths)
//     if (paths.indexOf(binding.value) === -1) {
//       el.parentNode.removeChild(el)
//     }
//   }
// })

Vue.prototype.axios = axios
Vue.prototype.qs = qs

Vue.prototype.$ELEMENT = {size: 'small'}

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
